// pages/used/used.js
const app = getApp({})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    class:[],
    handIndex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
        title:'请稍等',
    })
    wx.cloud.database().collection('subject').get({
        success:(res)=>{
            wx.hideLoading()
            console.log(res);
            var array = []
            if(res.data.length>0){
                for(var i =0;i<res.data.length;i++){
                    array.push(res.data[i].subjectClass)
                    this.setData({
                        class:array
                    })
                    console.log(this.data.class);
                }
            }
        },fail:(res)=>{
            wx.hideLoading()
        }
    })
  },

  click(e){
      var index =e.currentTarget.dataset.index
      this.setData({
          handIndex:index,
          name:this.data.class[index]
      })
      console.log(this.data.class[index]);
  },
  subMit(){
      wx.showModal({
        title: '确定选择'+this.data.class[this.data.handIndex],
        complete: (res) => {
          if (res.confirm) {
              wx.cloud.database().collection('user').where({
                  _openid:app.globalData.openid
              }).update({
                  data:{
                      direction:this.data.class[this.data.handIndex]
                  },success:(res_1)=>{
                      wx.setStorageSync('designed', this.data.class[this.data.handIndex])
                      wx.navigateBack({})
                  }
              })
          }
        }
      })
  }
})